package com.mg.flightplan.domain;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;


public class FlightPlanNvgt extends FlightPlan {

	public FlightPlanNvgt(File f){

		BufferedReader in;
		try {
			in = new BufferedReader(new FileReader(f));
	
			String strLine;
			while ((strLine = in.readLine()) != null) {
				System.out.println(strLine);
				if(strLine.startsWith("waypoint.")){
					int pos = strLine.indexOf("=");
					String wptString = strLine.substring(pos+1);
					
					String icao = wptString.split(",")[0];
					ICAOWaypoint wpt = new ICAOWaypoint(icao, "T?", new StdPositionConverter(wptString));

					ICAOWaypoint previousWpt;
					if (icaoList.size() > 0)
						previousWpt = icaoList.get(icaoList.size() - 1);
					else
						previousWpt = null;
					wpt.calculateFromPrevious(previousWpt);
					icaoList.add(wpt);

					System.out.println(wpt);
				}
			}
	
			in.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

	private class StdPositionConverter extends PositionConverter{
		
		
		public StdPositionConverter(String worldPosition){
			char latHem,lonHem;
			int latDeg, lonDeg, latMin, lonMin, latSec,lonSec;
			int pos, pos1;
			System.out.println("parse: "+worldPosition);
			String[] parts = worldPosition.split(",");
			String onepart=null;
			
			onepart=parts[2].trim(); //N43* 35.27'

			latHem = onepart.charAt(0);
			
			pos = onepart.indexOf("*");
			latDeg = Integer.parseInt(onepart.substring(1, pos));

			pos = onepart.indexOf(".");
			pos1 = onepart.indexOf(" "); // first space
			latMin = Integer.parseInt(onepart.substring(pos1+1, pos));

			latSec = Integer.parseInt(onepart.substring(pos+1, pos+3));



			onepart=parts[3].trim(); //N43* 35.27'

			lonHem = onepart.charAt(0);
			
			pos = onepart.indexOf("*");
			lonDeg = Integer.parseInt(onepart.substring(1, pos));

			pos = onepart.indexOf(".");
			pos1 = onepart.indexOf(" "); // first space
			lonMin = Integer.parseInt(onepart.substring(pos1+1, pos));

			lonSec = Integer.parseInt(onepart.substring(pos+1, pos+3));
			
			
			// Now we convert this to decimal degrees
			float decimalLatDegree = latDeg + latMin/60F + latSec/3600F ;
			float decimalLonDegree = lonDeg + lonMin/60F + lonSec/3600F ;
			
			// Now we convert this to radians
			radLat = (float) (decimalLatDegree * Math.PI / 180);
			radLon = (float) ( decimalLonDegree  * Math.PI / 180);
			if (latHem=='S') radLat=radLat*-1;
			if (lonHem=='E') radLon=radLon*-1;
			
			
		}
	}
		
}
